package com.heima.article.service.imp;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ArticleConfigMapper;
import com.heima.article.mapper.ArticleContentMapper;
import com.heima.article.mapper.ArticleMapper;
import com.heima.article.service.ArticleService;
import com.heima.common.exception.CustomException;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.Article;
import com.heima.model.article.pojos.ArticleConfig;
import com.heima.model.article.pojos.ArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 文章信息表，存储已发布的文章 服务实现类
 * </p>
 *
 * @author DX
 */
@Slf4j
@Service
@Transactional
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private ArticleConfigMapper configMapper;

    @Autowired
    private ArticleContentMapper contentMapper;
    /**
     * 根据参数加载文章列表
     * @param loadtype 1 为加载更新 2 为加载更多
     * @param dto
     * @return
     */
    @Override
    public ResponseResult load(Short loadtype, ArticleHomeDto dto) {
        //对数据进行校验
        if (loadtype == null){
            loadtype = 1;//加载更新
        }
        if (dto.getSize() == null || dto.getSize() == 0){
            dto.setSize(10);
        }
        //取最小值
        dto.setSize(Math.min(dto.getSize(),30));//设置默认30条
        //如果频道为空就查默认：最新数据
        if (StringUtils.isEmpty(dto.getTag())){
            dto.setTag("__all__");
        }
        if (dto.getMinBehotTime() == null && dto.getMaxBehotTime() == null){
            dto.setMinBehotTime(new Date(0));
            dto.setMaxBehotTime(new Date());//用当前时间
        }
        //调用Mapper查询数据
        List<Article> list = articleMapper.load(loadtype, dto);
        //返回结果
        return ResponseResult.okResult(list);
    }

    @Override
    public ResponseResult saveArticle(ArticleDto dto) {
        //校验参数
        if (dto == null || StringUtils.isEmpty(dto.getContent())){
           throw new CustomException(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        Article article = new Article();
        BeanUtils.copyProperties(dto,article);
        //判断文章id是否存在，存在就更新，不存在就保存
        if (dto.getId() == null){
            //新增
            save(article);
            //保存文章
            ArticleConfig articleConfig = new ArticleConfig();
            articleConfig.setArticleId(article.getId());
            articleConfig.setIsComment(true);//评论
            articleConfig.setIsDelete(false);//删除
            articleConfig.setIsDown(false);//下架
            articleConfig.setIsForward(true);//转发

            //插入数据
            configMapper.insert(articleConfig);
            //保存文章内容
            ArticleContent articleContent = new ArticleContent();
            articleContent.setArticleId(article.getId());
            articleContent.setContent(dto.getContent());
            //插入数据
            contentMapper.insert(articleContent);
        }else {
            //修改
            updateById(article);
            ArticleContent articleContent = contentMapper.selectOne(Wrappers.<ArticleContent>lambdaQuery().eq(ArticleContent::getArticleId, dto.getId()));
            articleContent.setContent(dto.getContent());
            contentMapper.updateById(articleContent);
        }
        //返回文章id
        return ResponseResult.okResult(article.getId());
    }
}
